package cn.springstudio.springui.template;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class LayoutFragment extends Fragment {

    @LayoutRes
    private int resource;
    private ViewListener listener;

    protected LayoutFragment(@LayoutRes int resource, ViewListener listener) {
        this.resource = resource;
        this.listener = listener;
    }

    public static LayoutFragment newInstance(@LayoutRes int resource) {
        return new LayoutFragment(resource, null);
    }

    public static LayoutFragment newInstance(@LayoutRes int resource, ViewListener listener) {
        return new LayoutFragment(resource, listener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(resource, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (listener != null) {
            listener.bindView(view);
        }
    }

    public LayoutFragment setViewListener(ViewListener listener) {
        this.listener = listener;
        return this;
    }
}